package com.example.word_book.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SPSave {
    private Context context;


    public SPSave(Context context) {
        this.context = context;
    }

    public static  boolean saveDateInfo(Context context, Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String str = format.format(date);
        System.out.println(str);
        SharedPreferences sharedPreferences = context.getSharedPreferences("dateInfo", Context.MODE_PRIVATE);
        String d = sharedPreferences.getString("date", "");
        if (!d.equals(str) || "".equals(d)) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("date", str);
            editor.commit();
            new SqlHelp(context).deleteExpireError();
            return false;
        }
        return true;
    }

    public static boolean saveUserInfo(Context context, String account, String password, boolean isCheck) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("account", account);
        editor.putString("password", password);
        if (isCheck)
            editor.putString("isCheck", "isCheck");
        else
            editor.putString("isCheck", "");
        editor.commit();

        return true;


    }

    public static Map<String, String> getUserInfo(Context context) {

        Map<String, String> userMap = new HashMap<>();

        SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        String password = sharedPreferences.getString("password", "");
        String isCheck = sharedPreferences.getString("isCheck", "");
        userMap.put("account", account);
        userMap.put("password", password);
        userMap.put("isCheck", isCheck);

        return userMap;
    }
}
